import java.util.Arrays;


public class Test {
    public static void reverse(int[] array){
        int i = 0;
        int j =array.length-1;
        while(i<j){
            int tmp = array[i];
            array[i]= array[j];
            array[j] = tmp;
            i++;
            j--;
        }

    }
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        reverse(array);
        System.out.println(Arrays.toString(array));

    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1 ; i++) {
            Boolean flg = false ;
            for (int j = 0; j <array.length-1-i ; j++) {
                if(array[j+1]<array[j]){
                    int tmp = array[j+1];
                    array[j+1]=array[j];
                    array[j]=tmp;
                    flg = true;
                }
            }
            if (!flg){
                break;
            }
        }
    }
    public static void main9(String[] args) {
        int[] array = {1,10,9,5,6};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main8(String[] args) {
        //利用工具进行二分查找
        int[] array = new int[]{1,2,3,4,5};
        int ret = Arrays.binarySearch(array, 5);
        System.out.println(ret);

    }
    public static void main7(String[] args) {
        int[] array = new int[]{1,22,31,44,5,8};
        // 1 5 8 22 31 44
        System.out.println("排序前"+Arrays.toString(array));
        Arrays.sort(array);
        System.out.println("排序后"+Arrays.toString(array));
    }
    public static void main6(String[] args) {
        int[] array = new int[]{1,22,31,44,5,8};
        // 1 5 8 22 31 44
        Arrays.sort(array);
        int data= 44;
        int ret = binarySearch(array, data);
        if (ret==-1){
            System.out.println("没有找到值");
        }else {
            System.out.println(ret);
        }
    }
    //二分查找
    public static int binarySearch(int[] array,int data){
        int i = 0;
        int j =array.length-1;

        while(i<=j){
            int mid = (i+j)/2;
            if(array[mid]>data){
                j = mid -1;
            } else if (array[mid]<data) {
                i = mid +1;
            }else{
                return i;
            }
        }
        return -1;
    }

    public static int fuc(int[] array,int data) {
        for (int i = 0; i <array.length ; i++) {
            if (array[i]==data){
                return i;
            }
        }
        return -1;

    }
    public static void main5(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        int data = 4;
        System.out.println(fuc(array, data));
    }


    public static void main4(String[] args) {

        int[] array = new int[]{1,2,3,4};
        int[] arr=Arrays.copyOfRange(array,1,3);
        System.out.println(Arrays.toString(arr));

    }
    public static void main3(String[] args) {

        int[] array = new int[]{1,2,3,4};
        int[] newarray= Arrays.copyOf(array, array.length);
        Arrays.copyOfRange(array,1,3);
        System.out.println(Arrays.toString(newarray));

    }
    //数组的拷贝
    public static void main2(String[] args) {
        int[] array1 = new int[]{1,2,3,4};
        int[] array2 = new int[array1.length];

        System.out.println("拷贝前："+Arrays.toString(array1));
        System.out.println("拷贝前："+Arrays.toString(array2));

        for (int i = 0; i <array1.length ; i++) {
            array2[i] = array1[i];

        }
        System.out.println("==========");
        System.out.println("拷贝后："+Arrays.toString(array1));
        System.out.println("拷贝后："+Arrays.toString(array2));




    }
    public static void main1(String[] args) {

        int[] array = {1, 2, 3, 4};
        System.out.println(Arrays.toString(array));

    }
}
